// 持久化存储模块

// 个人信息存储key
const INFO_KEY = 'fs_shopping_info'
// 历史记录持久或存储的key
const HISTORY_KEY = 'fs_search_history'
// 加入购物车的脚标持久化存储
const CART_KEY = 'fs_cart'

// 设置个人信息
export const setInfo = (info) => {
  localStorage.setItem(INFO_KEY, JSON.stringify(info))
}
// 获取个人信息
export const getInfo = () => {
  const res = localStorage.getItem(INFO_KEY)
  return res ? JSON.parse(res) : { token: '', userId: '' }
}

// 移除个人信息
export const removeInfo = () => {
  localStorage.removeItem(INFO_KEY)
}

// 获取搜索历史
export const getSearchHistory = () => {
  const res = localStorage.getItem(HISTORY_KEY)
  return res ? JSON.parse(res) : []
}
// 保存搜索历史
export const setSearchHistory = (history) => {
  localStorage.setItem(HISTORY_KEY, JSON.stringify(history))
}

// 移除搜索历史
export const removeSearchHistory = () => {
  localStorage.removeItem(HISTORY_KEY)
}

// 加入购物车持久化存储
export const getCart = () => {
  const res = localStorage.getItem(CART_KEY)
  return res ? JSON.parse(res) : []
}

// 保存加入购物车
export const setCart = (cart) => {
  localStorage.setItem(CART_KEY, JSON.stringify(cart))
}

// 移除加入购物车
export const removeCart = () => {
  localStorage.removeItem(CART_KEY)
}
